package com.jingdianjichi.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthRoleDTOConvert;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role/")
@Slf4j

public class RoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 角色添加
     * @param
     * @return
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色Key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名字不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTABCE.convertDTOTOBO(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("RoleController.add.dto.error:{}", e.getMessage(), e);
            return Result.fail("角色添加失败");
        }

    }

    /**
     * 角色更新
     * @param
     * @return
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色Id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTABCE.convertDTOTOBO(authRoleDTO);

            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.update.dto.error:{}", e.getMessage(), e);
            return Result.fail("更新角色信息失败");
        }

    }

    /**
     * 角色删除
     * @param
     * @return
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTABCE.convertDTOTOBO(authRoleDTO);

            return Result.ok(authRoleDomainService.delete(authRoleBO));
        } catch (Exception e) {
            log.error("UserController.delete.dto.error:{}", e.getMessage(), e);
            return Result.fail("删除角色失败");
        }

    }

}
